from LeetCoder import *


class Solution:
    def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]:
        c = [0] * n
        ans = [0] * len(queries)
        for i, query in enumerate(queries):
            idx, color = query[0], query[1]
            if c[idx] == color:
                ans[i] = ans[i-1] if i > 0 else 0
                c[idx] = color
            else:
                ans[i] = ans[i-1] if i > 0 else 0
                if idx > 0 and c[idx-1] == c[idx] and c[idx] != 0:
                    ans[i] -= 1
                if idx < n - 1 and c[idx] == c[idx+1] and c[idx] != 0:
                    ans[i] -= 1
                c[idx] = color
                if idx > 0 and c[idx-1] == c[idx]:
                    ans[i] += 1
                if idx < n - 1 and c[idx] == c[idx+1]:
                    ans[i] += 1
        return ans




s = Solution()
print(s.colorTheArray(n=4, queries=[[0, 2], [1, 2], [3, 1], [1, 1], [2, 1]]))
